package com.zHan.sky.controller.User;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zHan.sky.common.result.Result;
import com.zHan.sky.dto.OrdersPaymentDTO;
import com.zHan.sky.dto.OrdersSubmitDTO;
import com.zHan.sky.service.OrderDetailService;
import com.zHan.sky.service.OrdersService;
import com.zHan.sky.service.ShoppingCartService;
import com.zHan.sky.vo.OrderPaymentVO;
import com.zHan.sky.vo.OrderSubmitVO;
import com.zHan.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * ClassName: OrdersController
 * Description:
 * date: 2023/7/29 0029 19:16
 *
 * @author zHan
 */
@RestController("userOrderController")
@Slf4j
@RequestMapping("/user/order")
@Api(tags = "C端-订单相关接口")
public class OrderController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 用户下单
     *
     * @param ordersSubmitDTO
     * @return
     */
    @PostMapping("/submit")
    @ApiOperation(value = "用户下单")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
        OrderSubmitVO orderSubmitVO = ordersService.submit(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }

    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO,
                                          HttpServletResponse response) throws Exception {
        OrderPaymentVO orderPaymentVO = ordersService.payment(ordersPaymentDTO,response);
        return Result.success(orderPaymentVO);
    }

    /**
     * 历史订单查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/historyOrders")
    @ApiOperation(value = "历史订单查询")
    public Result<Page<OrderVO>> userPage(int page, int pageSize, Integer status) {
        Page<OrderVO> orderVOPage = ordersService.historyOrders(page, pageSize,status);
        return Result.success(orderVOPage);
    }

    /**
     * 再来一单
     *
     * @param id
     * @return
     */
    @PostMapping("/repetition/{id}")
    @ApiOperation(value = "再来一单")
    public Result<String> again(@PathVariable Long id) {
        ordersService.repetition(id);
        return Result.success();
    }

    /**
     * 查询订单详情
     * @param id
     * @return
     */
    @GetMapping("/orderDetail/{id}")
    @ApiOperation(value = "查询订单详情")
    public Result<OrderVO> orderDetail(@PathVariable Long id) {
        OrderVO orderVO = ordersService.details(id);
        return Result.success(orderVO);
    }

    /**
     * 用户取消订单
     *
     * @param id
     * @return
     */
    @PutMapping("/cancel/{id}")
    @ApiOperation(value = "用户取消订单")
    public Result cancel(@PathVariable Long id) {
        ordersService.userCancelOrderById(id);
        return Result.success();
    }

    /**
     * 客户催单
     * @param id
     * @return
     */
    @GetMapping("/reminder/{id}")
    public Result reminder(@PathVariable Long id) {
        ordersService.reminder(id);
        return Result.success();
    }

}
